package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.pojo.dto.CarUpdateInfoDTO;
import cn.tedu.fruitshop.pojo.dto.OrderAddCarDTO;
import cn.tedu.fruitshop.pojo.dto.OrdersAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.OrdersUpdateInfoDTO;
import cn.tedu.fruitshop.pojo.vo.OrdersListItemVO;
import cn.tedu.fruitshop.pojo.vo.OrdersStandardVO;
import cn.tedu.fruitshop.service.ICarService;
import cn.tedu.fruitshop.service.IOrdersService;
import cn.tedu.fruitshop.service.impl.CarServiceImpl;
import cn.tedu.fruitshop.service.impl.ItemCategoryServiceImpl;
import cn.tedu.fruitshop.web.JsonResult;
import cn.tedu.fruitshop.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "07. 订单管理模块")
@RequestMapping("/orders")

public class OrdersController {

    @Autowired
    IOrdersService ordersService;

    public OrdersController() {
        log.debug("创建控制器对象：OrdersController");
    }

    @ApiOperation("添加订单001")
    @ApiOperationSupport(order = 101)
    @PostMapping("/add-order")
    public JsonResult orderAdd(@Valid @RequestBody OrderAddCarDTO orderAddCarDTO) {
        if(orderAddCarDTO.getDeliveryAddress().isEmpty()){
            return JsonResult.fail(ServiceCode.ERR_NOT_FOUND,"地址不能为空");
        }
        log.debug("开始处理【添加订单】的请求，参数为:{}", orderAddCarDTO);
        ordersService.addOrder(orderAddCarDTO);
        log.debug("添加订单成功");

        return JsonResult.ok();
    }

    @ApiOperation("添加订单")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid OrdersAddNewDTO ordersAddNewDTO) {
        log.debug("开始处理【添加订单】的请求，参数为:{}", ordersAddNewDTO);
        ordersService.addNew(ordersAddNewDTO);
        log.debug("添加订单成功");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除订单")
    @ApiOperationSupport(order = 200)
    @PostMapping("/delete")
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "Integer")
    public JsonResult delete(@RequestParam(value = "id") Integer id) {
        log.debug("开始处理【删除订单】的请求，参数为:{}", id);
        ordersService.delete(id);
        log.debug("删除订单成功!");
        return JsonResult.ok();
    }

    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<OrdersListItemVO>> list() {
        log.debug("开始处理【查询订单列表】的请求，无参数");
        List<OrdersListItemVO> list = ordersService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据id查询订单列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<List<OrdersStandardVO>> getByUserId(@PathVariable Long id) {
        log.debug("开始处理【根据id查询订单列表】的请求，参数：{}", id);
        List<OrdersStandardVO> orders = ordersService.getByUserId(id);
        return JsonResult.ok(orders);
    }

    @ApiOperation("根据id修改订单详情")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "Integer")
    @PostMapping("/{id:[0-9]+}/updateById")
    public JsonResult<Void> updateById(@PathVariable Integer id, OrdersUpdateInfoDTO ordersUpdateInfoDTO) {
        log.debug("开始处理【修改订单详情】的请求，参数ID：{}, 新数据：{}", id, ordersUpdateInfoDTO);
        ordersService.updateInfoById(id, ordersUpdateInfoDTO);
        return JsonResult.ok();
    }

}
